//
//  PTSendMsg.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/25.
//

import SwiftUI

struct PTSendMsg: View {
    @State private var phone = ""
    @State private var msgContent = #"请到"信息"中编辑短信内容"#
    @State private var theme = ""
    @State private var isMediaMsg = false
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "暂不支持"
    
    var body: some View {
        ZStack {
            ScrollView {
                VStack {
                    Text("输入电话号码")
                        .frame(maxWidth: .infinity, alignment: .topLeading)
                        .padding(.leading,10)
                    TextField("请输入", text: $phone)
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.gray, lineWidth: 1)
                                .frame(height: 40)
                        )
                        .padding(.leading,10)
                        .padding(.trailing,10)
                    
                    Text("短信内容")
                        .frame(maxWidth: .infinity, alignment: .topLeading)
                        .padding(.leading,10)
                    
                    TextField("请输入", text: $msgContent)
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.gray, lineWidth: 1)
                                .frame(height: 40)
                        )
                        .padding(.leading,10)
                        .padding(.trailing,10)
                        .padding(.bottom,20)
                    
                    Button {
                        makeSysMessage()
                    } label: {
                        Text("调用自带发送短信")
                            .foregroundColor(.white)
                            .frame(maxWidth: .infinity)
                            .background(
                                RoundedRectangle(cornerRadius: 20)
                                    .fill(Color.blue)
                                    .frame(height: 40)
                            )
                    }
                    .buttonStyle(.plain)
                    .padding()
                    Spacer()
                }
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }
    }
    
    func makeSysMessage() -> Void {
        if phone.isEmpty {
            detailTxt = "请输入有效的电话号码"
            showHud()
            return
        }
        guard let phoneURL = URL(string: "sms://\(phone)") else {
            return
        }
        WKExtension.shared().openSystemURL(phoneURL)
    }
    
    private func showHud() {
        isShowingHUD = true
        DispatchQueue.main.asyncAfter(deadline: .now()+2){
            isShowingHUD = false
        }
    }
}

struct PTSendMsg_Previews: PreviewProvider {
    static var previews: some View {
        PTSendMsg()
    }
}
